package com.github.cdisk.xtrans.connect;

import java.sql.SQLException;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.lang.StringUtils;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.github.cdisk.xtrans.core.BaseComponent;
import com.github.cdisk.xtrans.core.GlobalVariables;
import com.github.cdisk.xtrans.exception.NoVariableNameException;

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = BaseComponent.class)
public class Connect {

	private String id;

	private String color;

	private String charset;

	/** 数据库连接驱动 */
	private String driver;

	/** 数据库连接地址 */
	private String url;

	/** 数据库连接用户 */
	private String user;

	/** 数据库连接密码 */
	private String password;

	@JsonIgnore
	private Db db;


	public void init(GlobalVariables variables) throws SQLException, NoVariableNameException, ClassNotFoundException {

		driver = variables.replaceVariables(driver);
		if (StringUtils.isEmpty(driver))
			throw new SQLException("connect(" + id + ")未指定[driver]内容.");

		url = variables.replaceVariables(url);
		if (StringUtils.isEmpty(url))
			throw new SQLException("connect(" + id + ")未指定[url]内容.");

		charset = variables.replaceVariables(charset);
		user = variables.replaceVariables(user);
		password = variables.replaceVariables(password);

		Class.forName(driver);
		db = new Db(this);
	}

	/**
	 * 解析连接结构
	 * 
	 * @param parent
	 * @return
	 * @throws ParserConfigurationException
	 */
	public static Connect Parse(Node parent) throws ParserConfigurationException {

		String parent_name = parent.getNodeName();
		if (!"connect".equals(parent_name)) {
			return null;
		}

		Connect connect = new Connect();
		NamedNodeMap map = parent.getAttributes();
		Node id = map.getNamedItem("id");
		connect.id = id.getNodeValue();
		NodeList nodes = parent.getChildNodes();
		int nodes_len = nodes.getLength();
		for (int nIndex = 0; nIndex < nodes_len; nIndex++) {
			Node node = nodes.item(nIndex);
			String node_name = node.getNodeName();

			if ("driver".equals(node_name)) {
				connect.driver = node.getTextContent();
			} else if ("url".equals(node_name)) {
				connect.url = node.getTextContent();
			} else if ("user".equals(node_name)) {
				connect.user = node.getTextContent();
				if (connect.user != null && connect.user.length() == 0)
					connect.user = null;
			} else if ("password".equals(node_name)) {
				connect.password = node.getTextContent();
				if (connect.password != null && connect.password.length() == 0)
					connect.password = null;
			} else if ("charset".equals(node_name)) {
				connect.charset = node.getTextContent();
				if (connect.charset != null && connect.charset.length() == 0)
					connect.charset = null;
			}
		}

		return connect;
	}

	/**
	 * 拼接用于保存的XML字符串
	 * Generate XML strings for saving
	 * @return
	 */
	public String toXml() {
		String xml = "";
		xml += "\t\t<connect id=\"" + id + "\">\n";
		xml += "\t\t\t<driver>" + StringUtils.trimToEmpty(driver) + "</driver>\n";
		xml += "\t\t\t<url>" + StringUtils.trimToEmpty(url) + "</url>\n";
		xml += "\t\t\t<user>" + StringUtils.trimToEmpty(user) + "</user>\n";
		xml += "\t\t\t<password>" + StringUtils.trimToEmpty(password) + "</password>\n";
		xml += "\t\t\t<charset>" + StringUtils.trimToEmpty(charset) + "</charset>\n";
		xml += "\t\t</connect>\n\n";
		return xml;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public String getCharset() {
		return charset;
	}

	public void setCharset(String charset) {
		this.charset = charset;
	}

	public String getDriver() {
		return driver;
	}

	public void setDriver(String driver) {
		this.driver = driver;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Db getDb() {
		return db;
	}

	public void setDb(Db db) {
		this.db = db;
	}

}
